import {
    reqAddArticleFromWangEditor,
    reqDeleteArticleByWangEditor,
    reqGetArticle,
    reqGetArticleByPage,
    reqGetArticleCount,
    reqGetArticleId
} from "../api/article";
import {requestGetData, requestIsSuccess} from "@/api/request";

export class Article {
    id:string;
    article?:string;
    file?:[];
    createtime?:string;
    intro?:string;
    title?:string;
    userinfoid?:string;
    constructor({id,article,file,createtime,intro,title,userinfoid}:{
        id:string,
        article?:string,
        file?:[],
        createtime?:string,
        intro?:string,
        title?:string,
        userinfoid?:string}) {
        this.id=id;
        this.article=article
        this.file=file
        this.createtime=createtime
        this.intro=intro
        this.title=title
        this.userinfoid=userinfoid    
    }

    /**
     * 删除文章对象，成功返回true
     * @returns {Promise<boolean>}
     */
    async deleteArticle() {
        let result = await reqDeleteArticleByWangEditor(this.id)
        return result
    }

    /**
     * 更新article，成功返回true
     * @returns {Promise<void>}
     */
    async updateArticle(){
        const result = await reqAddArticleFromWangEditor(this);
        return result
    }

    /**
     * 获得文章个数，失败返回 undefined
     * @returns {Promise<number|*>}
     */
    static async getArticleCount(){
        const result = await reqGetArticleCount();
        return result.data
    }

    /**
     * 分页获取文章列表
     * @param currentPage 当前页
     * @param pageSize 每页个数
     * @returns {Promise<[]|*[]>}
     */
     static async getArticleByPage(currentPage:number, pageSize:number) {
         const result = await reqGetArticleByPage(currentPage, pageSize);
         let articleList = result.data.map(tArticle => {
            return (new Article(tArticle))
        })
        return articleList
     }

    /**
     * 通过Id获取Article
     * @param id
     * @returns {Promise<*|Article>}
     */
     static async getArticleById(id:string):Promise<Article>{
        const result = await reqGetArticle(id);
        return new Article(result.data);
     }

    /**
     * 在服务端创建一个文章对象
     * @returns {Promise<*|Article>}
     */
     static async createArticle():Promise<Article>{
         const result = await reqGetArticleId();
         let id = result.data
         return new Article({id,article:"",title:"",intro:""})
     }
}